package com.longrou.base.base

import android.app.Activity
import android.os.Build
import android.os.Bundle
import android.support.v4.view.ViewCompat
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.view.WindowManager
import android.widget.FrameLayout
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.launcher.ARouter
import com.longrou.base.util.ToastUtils
import com.scwang.smartrefresh.layout.api.RefreshLayout

/**
 * @Description:
 * @Author: LongRou
 * @CreateDate: 2018-05-17 23:17
 * @Version: 1.0
 **/
open class BaseActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    protected fun buildActivity(path: String): Postcard {

        return ARouter.getInstance().build(path)
    }

    override fun onBackPressed() {
        super.onBackPressed()
        finish()
    }

    //Toast展示
    protected fun showToast(tips: String) {
        ToastUtils.showToast(this,tips)
        //Toast.makeText(this, tips, Toast.LENGTH_SHORT).show()
    }

    //沉浸式状态栏
    protected fun setImmerseStatusBar(statusColor: Int) {
        if (android.os.Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
            val window = this.getWindow()
            //设置透明状态栏,这样才能让 ContentView 向上
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)

            //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            //设置状态栏颜色
            window.setStatusBarColor(statusColor)

            val mContentView = this.findViewById(Window.ID_ANDROID_CONTENT) as ViewGroup
            val mChildView = mContentView.getChildAt(0)
            if (mChildView != null) {
                //注意不是设置 ContentView 的 FitsSystemWindows, 而是设置 ContentView 的第一个子 View . 使其不为系统 View 预留空间.
                ViewCompat.setFitsSystemWindows(mChildView, false)
            }
        } else {
            val window = this.getWindow()
            val mContentView = this.findViewById(Window.ID_ANDROID_CONTENT) as ViewGroup

            //首先使 ChildView 不预留空间
            var mChildView: View? = mContentView.getChildAt(0)
            if (mChildView != null) {
                ViewCompat.setFitsSystemWindows(mChildView, false)
            }
            val statusBarHeight = getStatusBarHeight(this)
            //需要设置这个 flag 才能设置状态栏
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            //避免多次调用该方法时,多次移除了 View
            if (mChildView != null && mChildView!!.getLayoutParams() != null && mChildView!!.getLayoutParams().height === statusBarHeight) {
                //移除假的 View.
                mContentView.removeView(mChildView)
                mChildView = mContentView.getChildAt(0)
            }
            if (mChildView != null) {
                val lp = mChildView!!.getLayoutParams() as FrameLayout.LayoutParams
                //清除 ChildView 的 marginTop 属性
                if (lp.topMargin >= statusBarHeight) {
                    lp.topMargin -= statusBarHeight
                    mChildView!!.setLayoutParams(lp)
                }
            }
        }
    }

    //着色式状态栏
    protected fun setColoringStatusBar(statusColor: Int) {
        if (android.os.Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
            val window = this.getWindow()
            //取消设置透明状态栏,使 ContentView 内容不再覆盖状态栏
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)

            //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            //设置状态栏颜色
            window.setStatusBarColor(statusColor)

            val mContentView = this.findViewById(Window.ID_ANDROID_CONTENT) as ViewGroup
            val mChildView = mContentView.getChildAt(0)
            if (mChildView != null) {
                //注意不是设置 ContentView 的 FitsSystemWindows, 而是设置 ContentView 的第一个子 View . 预留出系统 View 的空间.
                ViewCompat.setFitsSystemWindows(mChildView, true)
            }
        } else {
            val window = this.getWindow()
            val mContentView = this.findViewById(Window.ID_ANDROID_CONTENT) as ViewGroup

            //First translucent status bar.
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            val statusBarHeight = getStatusBarHeight(this)

            val mChildView = mContentView.getChildAt(0)
            if (mChildView != null) {
                val lp = mChildView.layoutParams as FrameLayout.LayoutParams
                //如果已经为 ChildView 设置过了 marginTop, 再次调用时直接跳过
                if (lp.topMargin < statusBarHeight && lp.height != statusBarHeight) {
                    //不预留系统空间
                    ViewCompat.setFitsSystemWindows(mChildView, false)
                    lp.topMargin += statusBarHeight
                    mChildView.layoutParams = lp
                }
            }

            var statusBarView: View? = mContentView.getChildAt(0)
            if (statusBarView != null && statusBarView.layoutParams != null && statusBarView.layoutParams.height == statusBarHeight) {
                //避免重复调用时多次添加 View
                statusBarView.setBackgroundColor(statusColor)
                return
            }
            statusBarView = View(this)
            val lp = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusBarHeight)
            statusBarView.setBackgroundColor(statusColor)
            //向 ContentView 中添加假 View
            mContentView.addView(statusBarView, 0, lp)
        }
    }

    private fun getStatusBarHeight(activity: Activity): Int {
        val resources = activity.getResources()
        val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
        val height = resources.getDimensionPixelSize(resourceId)
        return height
    }

    fun back(){
        finish()
    }

    fun openActivity(path: String) {
        ARouter.getInstance().build(path).navigation()
    }

    fun openActivityAndFinish(path: String) {
        ARouter.getInstance().build(path).navigation()
        finish()
    }

    open fun dataOnRefresh(refreshlayout : RefreshLayout){

    }
    open fun dataOnLoadmore(refreshlayout : RefreshLayout){

    }
}